package k2_oop.p9_collection.demo11;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class MainApp {

    private static Stream<Transaction> transactionStream;

    //在静态块中添加交易对象
    static {

        //初始化空的交易列表
        List<Transaction> transactionList;

        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");
        transactionList = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );

        transactionStream = transactionList.stream();
    }

    /**
     * 1.找出2011年发生的所有交易，并按交易额排序（从高到低）
     */
    public static void first(){

        transactionStream
                .filter(transaction -> transaction.getYear()==2011)
                .sorted((t1,t2)->t2.getValue()-t1.getValue())
                .forEach(System.out::println);

    }

    /**
     * 2.交易员都在哪些不同的城市工作过
     */
    public static void second(){
        transactionStream
                .map(transaction -> transaction.getTrader().getCity())
                .distinct()
                .forEach(System.out::println);
    }

    /**
     * 3.查找所有来自Cambridge的交易员，并按姓名排序
     */
    public static void third(){

        transactionStream
                .map(Transaction::getTrader)
                .filter(trader->trader.getCity().equals("Cambridge"))
                .sorted((t1,t2)->t1.getName().compareTo(t2.getName()))
                .forEach(System.out::println);
    }

    /**
     * 4.返回所有交易员的姓名字符串，按字母顺序排序
     */
    public static void fourth(){
        transactionStream
                .map(transaction -> transaction.getTrader().getName())
                .distinct()
                .sorted((n1,n2)->n1.compareTo(n2))
                .forEach(System.out::println);
    }

    /**
     * 5.有没有交易员在Milan工作
     */
    public static void fifth(){

        //输出所有交易员在Milan工作的数据
//        transactionStream
//                .map(Transaction::getTrader)
//                .filter(trader -> trader.getCity().equals("Milan"))
//                .forEach(System.out::println);

        boolean boo = transactionStream
                .map(transaction -> transaction.getTrader().getCity())
                .anyMatch(city->city.equals("Milan"));
        System.out.println(boo);

    }

    /**
     * 6.打印生活在Cambridge的交易员的所有交易额
     */
    public static void sixth(){

        transactionStream
                .filter(transaction -> transaction.getTrader().getCity().equals("Cambridge"))
//                .map(Transaction::getValue)
                .forEach(transaction -> System.out.println(transaction.getValue()));

    }

    /**
     * 7.所有的交易额中，最高的交易额是多少
     */
    public static void seventh(){

        int maxValue = transactionStream
                .map(Transaction::getValue)
                .max((v1,v2)->v1-v2)
                .get();

        System.out.println(maxValue);

    }

    /**
     * 8.所有的交易额中，找到交易额最小的交易
     */
    public static void eighth(){
        Transaction transaction = transactionStream
                .min((t1,t2)->t1.getValue()-t2.getValue())
                .get();

        System.out.println(transaction);
    }

    public static void main(String[] args) {

//        MainApp.first();
//        MainApp.second();
//        MainApp.third();
//        MainApp.fourth();
//        MainApp.fifth();
//        MainApp.sixth();
//        MainApp.seventh();
        MainApp.eighth();

    }

}
